<template>
	<div class="tabelContainer">
		<basic-container>
			<avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList" :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">

				<template slot="menuLeft">
					<el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permissionList.delBtn" @click="handleDelete">删 除</el-button>
					<el-button type="warning" size="small" plain icon="el-icon-download" v-if="permissionList.exportBtn" @click="handleExport">导出</el-button>
					<el-button size="small" plain v-if="permissionList.signInBtn" @click="signIn">签 到</el-button>
					<el-button size="small" plain v-if="permissionList.notSignInBtn" @click="notSignIn">未签到</el-button>
				</template>

				<template slot="isSignIn" slot-scope="scope">
					<div v-if="scope.row.isSignIn === 1">已签到</div>
					<div v-else-if="scope.row.isSignIn === 0">未签到</div>
					<div v-else>未知</div>
				</template>
			</avue-crud>
		</basic-container>
	</div>
</template>

<script>
	import { getList, getDetail, add, update, remove, sign, unSign } from "@/api/dust/trainrecord";
	import { mapGetters } from "vuex";
	import { getToken } from '@/util/auth';

	export default {
		data() {
			return {
				form: {},
				query: {},
				loading: true,
				page: {
					pageSize: this.$pageSize,
					currentPage: 1,
					total: 0
				},
				selectionList: [],
				option: {
					// height:'auto',
					calcHeight: 30,
					tip: false,
					searchShow: true,
					searchMenuSpan: 4,
					menu: false,
					border: true,
					index: false,
					viewBtn: true,
					selection: true,
					dialogClickModal: false,
					column: [{
							label: "项目",
							prop: "deptId", //deptInfoId
							type: "select",
							dicUrl: "/api/squirrel-ficm/deptinfo/getProjectList",
							props: {
								label: "deptName",
								value: "id"
							},
							hide: true,
							search: true,
							searchSpan: 5,
							searchLabelWidth: 50,
							searchPlaceholder: "请选择",
							rules: [{
								required: true,
								message: "请输入项目名称，中文",
								trigger: "blur"
							}]
						},
						{
							label: "工号",
							prop: "workNum",
							rules: [{
								required: true,
								message: "请输入工号",
								trigger: "blur"
							}]
						},
						{
							label: "姓名",
							prop: "staffName",
							rules: [{
								required: true,
								message: "请输入员工姓名",
								trigger: "blur"
							}]
						},
						{
							label: "是否签到",
							prop: "isSignIn",
							slot: true,
							rules: [{
								required: true,
								message: "请输入是否签到",
								trigger: "blur"
							}]
						},
						{
							label: "岗位",
							prop: "postName",
							rules: [{
								required: true,
								message: "请输入岗位",
								trigger: "blur"
							}]
						},
						{
							label: "培训课程",
							prop: "trainCourse",
							rules: [{
								required: true,
								message: "请输入培训课程",
								trigger: "blur"
							}]
						},
						{
							label: "培训计划",
							prop: "planName",
							search: true,
							searchSpan: 6,
							searchLabelWidth: 75,
							searchPlaceholder: "请输入",
							rules: [{
								required: true,
								message: "请输入培训计划",
								trigger: "blur"
							}]
						},
						{
							label: "所属项目",
							prop: "deptName",
							type: "select",
							rules: [{
								required: true,
								message: "请输入项目名称",
								trigger: "blur"
							}]
						},
					]
				},
				data: []
			};
		},
		computed: {
			...mapGetters(["permission"]),
			permissionList() {
				return {
					addBtn: this.vaildData(this.permission.trainrecord_add, false),
					viewBtn: this.vaildData(this.permission.trainrecord_view, false),
					delBtn: this.vaildData(this.permission.trainrecord_delete, false),
					editBtn: this.vaildData(this.permission.trainrecord_edit, false),
					exportBtn: this.vaildData(this.permission.trainrecord_export, false),
					signInBtn: this.vaildData(this.permission.trainrecord_signIn, false),
					notSignInBtn: this.vaildData(this.permission.trainrecord_notSignIn, false),
				};
			},
			ids() {
				let ids = [];
				this.selectionList.forEach(ele => {
					ids.push(ele.id);
				});
				return ids.join(",");
			}
		},
		methods: {
			signIn() {
				if(this.selectionList.length === 0) {
					this.$message.warning("请选择至少一项");
					return;
				}
				this.$confirm("确定将所选人员设为签到?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						for(var i = 0, len = this.selectionList.length; i < len; i++) {
							this.selectionList[i].isSignIn = 1;
						}
					})
					.then(() => {
						return sign(this.ids);
					})
					.then(() => {
						this.onLoad(this.page);
						this.$refs.crud.toggleSelection();
						this.$message({
							type: "success",
							message: "修改成功!"
						});
					});
			},
			notSignIn() {
				if(this.selectionList.length === 0) {
					this.$message.warning("请选择至少一项");
					return;
				}
				this.$confirm("确定将所选人员设为未签到?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						for(var i = 0, len = this.selectionList.length; i < len; i++) {
							this.selectionList[i].isSignIn = 0;
						}
					})
					.then(() => {
						return unSign(this.ids);
					})
					.then(() => {
						this.onLoad(this.page);
						this.$refs.crud.toggleSelection();
						this.$message({
							type: "success",
							message: "修改成功!"
						});
					});
			},
			handleExport() {
				this.$confirm("是否导出用户数据?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(() => {
					const searchForm = this.$refs.crud.searchForm;
					window.open(`/api/squirrel-train/trainrecord/export?${this.website.tokenHeader}=${getToken()}&deptId=${searchForm.deptId}&planName=${searchForm.planName}`);
				});
			},
			rowSave(row, done, loading) {
				add(row).then(() => {
					this.onLoad(this.page);
					this.$message({
						type: "success",
						message: "操作成功!"
					});
					done();
				}, error => {
					loading();
					window.console.log(error);
				});
			},
			rowUpdate(row, index, done, loading) {
				update(row).then(() => {
					this.onLoad(this.page);
					this.$message({
						type: "success",
						message: "操作成功!"
					});
					done();
				}, error => {
					loading();
					console.log(error);
				});
			},
			rowDel(row) {
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(row.id);
					})
					.then(() => {
						this.onLoad(this.page);
						this.$message({
							type: "success",
							message: "操作成功!"
						});
					});
			},
			handleDelete() {
				if(this.selectionList.length === 0) {
					this.$message.warning("请选择至少一条数据");
					return;
				}
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(this.ids);
					})
					.then(() => {
						this.onLoad(this.page);
						this.$message({
							type: "success",
							message: "操作成功!"
						});
						this.$refs.crud.toggleSelection();
					});
			},
			beforeOpen(done, type) {
				if(["edit", "view"].includes(type)) {
					getDetail(this.form.id).then(res => {
						this.form = res.data.data;
					});
				}
				done();
			},
			searchReset() {
				this.query = {};
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.query = params;
				this.page.currentPage = 1;
				this.onLoad(this.page, params);
				done();
			},
			selectionChange(list) {
				this.selectionList = list;
			},
			selectionClear() {
				this.selectionList = [];
				this.$refs.crud.toggleSelection();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			refreshChange() {
				this.onLoad(this.page, this.query);
			},
			onLoad(page, params = {}) {
				this.loading = true;
				getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
					let data = res.data.data;
					this.page.total = data.total;
					this.data = data.records;
					this.loading = false;
					this.selectionClear();
				});
			}
		}
	};
</script>

<style scoped>

</style>